﻿Function Get-Neo4jCredential($userName,$password)
{
    $b  = [System.Text.Encoding]::UTF8.GetBytes($userName + ":" + $password)
    $credential=[System.Convert]::ToBase64String($b)
    $credential="Basic " + $credential
    $credential

}

Function New-Neo4jNode($serverName,$userName,$password,[hashtable]$properties)
{
    $credential=Get-Neo4jCredential -userName $userName -password $password

    $body='{'

    $counter=0

    foreach($property in $properties.GetEnumerator())
    {
        $counter++
        $body+='"' + $property.Key + '":"' + $property.Value + '"'
        if($counter -lt $properties.Count)
        {
            $body+=','
        }
    }

    $body+='}'

    Invoke-RestMethod -Method Post -Uri http://$serverName`:7474/db/data/node/ -ContentType "application/json" -Body $body -Headers @{"Authorization"=$credential}
}

Function Set-Neo4jNodeLabel($serverName,$userName,$password,$nodeId,[array]$labels)
{
    $credential=Get-Neo4jCredential -userName $userName -password $password

    $body='['

    $counter=0

    foreach($label in $labels)
    {
        $counter++
        $body+='"' + $label + '"'
        if($counter -lt $labels.Count)
        {
            $body+=','
        }
    }

    $body+=']'

    Invoke-RestMethod -Method Post -Uri http://$serverName`:7474/db/data/node/$nodeId/labels -ContentType "application/json" -Body $body -Headers @{"Authorization"=$credential}
}

Function Remove-Neo4jNodeLabel($serverName,$userName,$password,$nodeId,$label)
{
    $credential=Get-Neo4jCredential -userName $userName -password $password

    Invoke-RestMethod -Method Delete -Uri http://$serverName`:7474/db/data/node/$nodeId/labels/$label -Headers @{"Authorization"=$credential}
}

Function New-Neo4jRelationship($serverName,$userName,$password,$sourceNodeId,$targetNodeId,$type)
{
    $credential=Get-Neo4jCredential -userName $userName -password $password

    $existingRelationshipQuery="MATCH (s)-[r]-(t) WHERE id(s) = $sourceNodeId AND id(t) = $targetNodeId return id(r)"

    $existingRelationship=Get-Neo4jCypherQueryResult -serverName $serverName -userName $userName -password $password -cypherQuery $existingRelationshipQuery

    if($existingRelationship.results.Count -eq 1)
    {
        if($existingRelationship.results[0].data.Count -eq 0)
        {

            $body='{
                      "to" : "http://'+$serverName+':7474/db/data/node/'+ $targetNodeId +'",
                      "type" : "'+ $type +'"
                    }'
    
            $relationship=Invoke-RestMethod -Method Post -Uri http://$serverName`:7474/db/data/node/$sourceNodeId/relationships -ContentType "application/json" -Body $body -Headers @{"Authorization"=$credential}
        }
    }

    $relationship
}

Function Remove-Neo4jNode($serverName,$userName,$password,$nodeId)
{
    $credential=Get-Neo4jCredential -userName $userName -password $password

    Invoke-RestMethod -Method Delete -Uri http://$serverName`:7474/db/data/node/$nodeId -Headers @{"Authorization"=$credential}
}

Function Get-Neo4jNodeProperties($serverName,$userName,$password,$nodeId)
{
    $credential=Get-Neo4jCredential -userName $userName -password $password

    Invoke-RestMethod -Method Get -Uri http://$serverName`:7474/db/data/node/$nodeId/properties -Headers @{"Authorization"=$credential}
}

Function Get-Neo4jCypherQueryResult($serverName,$userName,$password,$cypherQuery)
{
    $credential=Get-Neo4jCredential -userName $userName -password $password


    $body='{
              "statements" : [ {
                "statement" : "'+ $cypherQuery +'"
                }]
            }'
    
    

    Invoke-RestMethod -Method Post -Uri http://$serverName`:7474/db/data/transaction -ContentType "application/json" -Headers @{"Authorization"=$credential} -Body $body
}


